#include "stdafx.h"
#include "outputIsoforms.h"
using namespace std;
int printFasta(ostream& out, const char* tag, const char* seq)
{
    const int NT_PER_LINE = 60;
    int i;
    out << '>' << tag << endl;
    for (i = 0; seq[i] != '\0'; i++) {
        out << seq[i];
        if ( i % NT_PER_LINE == NT_PER_LINE - 1) {
            out << endl; // in case double new line
        }
    }
    if ( i % NT_PER_LINE != 0 ) {
        out << endl; // in case double new line
    }
    return(0);
}

// print vector value for debug
void printIntVector(vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << ',';
    }
    cout << endl;
}

int printSeqsInFasta(const char* ofileName, set<string> seqs, string seqPrefix, bool newFile)
{
    ofstream out;
    if (newFile) {
        out.open(ofileName);
    } else {
        out.open(ofileName, ios::app);
    }
    int i = 0;
    for (set<string>::iterator it = seqs.begin();
            it != seqs.end(); it++, i++) {
        char tag[FILENAME_MAX];
        int seq_length = strlen(it->c_str());
        sprintf(tag, "%s_%d %d", seqPrefix.c_str(), i, seq_length);
        printFasta(out, tag, it->c_str());
    }
    out.close();
    return(seqs.size());
}

int printCDSs(const char* ofileName, set<string> CDSs, string geneName, bool possitiveStrand)
{
    char seq_tag_prefix[FILENAME_MAX];
    char strand = possitiveStrand ? 'F' : 'R';
    sprintf(seq_tag_prefix, "CDS_%c_%s", strand, geneName.c_str());
    return(printSeqsInFasta(ofileName, CDSs, string(seq_tag_prefix), possitiveStrand));
}

// print number pair serious as the exons
int printIsoformsExons(const char* ofileN, bool newFile, vector<CTIsoform>& isoforms)
{
    ofstream ofile;
    if (newFile) {
        ofile.open(ofileN);
    } else {
        ofile.open(ofileN, ios::app);
    }
    int returnV = printIsoformsExons(ofile, isoforms);
    ofile.close();
    return(returnV);
}

// print in serious number pairs.
int printIsoformsExons(ostream& out, vector<CTIsoform>& isoforms)
{
    unsigned int i;
    for (i = 0; i < isoforms.size(); i++) {
        char strand = isoforms.at(i).forward ? 'F' : 'R';
        char isoformID[MAX_LINE];
        sprintf(isoformID, "I%c%d", strand, i);
        out << isoformID;
        isoforms.at(i).printIsoform(out);
    }
    out << endl;
    return(i);
}

void reportIsoform(ostream& out, CIsoformInfo& isoformInfo)
{
    char tag[FILENAME_MAX];
    int exonNo = (int)isoformInfo.exons.size();
    sprintf(tag, "%s %s %d %s %d %s %d %s %d", isoformInfo.isoformId.c_str(),
            " aaSize ", isoformInfo.aaSize,
            " ntSize ", (int)isoformInfo.seq.length(),
            " CDSscore ", isoformInfo.CDSscore,
            " ExonNo ", exonNo);
    printFasta(out, tag , isoformInfo.seq.c_str());
}

int printIsoforms(ostream& out, vector<CIsoformInfo>& isoforms)
{
    unsigned int i = 0;
    for (i = 0; i < isoforms.size(); i++) {
        reportIsoform(out, isoforms.at(i));
    }
    return(i);
}

int printIsoforms(const char* ofileN, bool newFile, vector<CIsoformInfo>& isoforms)
{
    ofstream ofile;
    if (newFile) {
        ofile.open(ofileN);
    } else {
        ofile.open(ofileN, ios::app);
    }
    int returnV = printIsoforms(ofile, isoforms);
    ofile.close();
    return (returnV);
}

